package cn.com.xf.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.xf.dao.TblCheckReportDAO;
import cn.com.xf.dao.TsUserDAO;
import cn.com.xf.domain.ReportSummary;
import cn.com.xf.domain.TblCheckReport;
import cn.com.xf.domain.TsUser;
import cn.com.xf.service.ReportService;

@Service
public class ReportServiceImpl implements ReportService {

	@Autowired
	private TblCheckReportDAO reportDAO;
	@Autowired
	private TsUserDAO userDAO;
	
	@Override
	public List<TblCheckReport> selectReport(int begin, int pageSize,
			String userid, String siteid, String deptid, Date startdate, Date enddate) {
		return reportDAO.select(begin, pageSize, userid, siteid, deptid, startdate, enddate);
	}

	@Override
	public int selectReportCount(String userid, String siteid, String deptid, Date startdate, Date enddate) {
		return reportDAO.selectCount(userid, siteid, deptid, startdate, enddate);
	}

	@Override
	public List<ReportSummary> getSummary(String userid, String siteid,
			String deptid, Date month){
		List<ReportSummary> list = new ArrayList<ReportSummary>();
		if (month != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(month);
			int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
			Date startdate = calendar.getTime();
			calendar.add(Calendar.DAY_OF_MONTH, days - 1);
			Date enddate = calendar.getTime();
			if (!userid.equals("")) {
				list= userSummary(userid, startdate, enddate);
			} else if (!deptid.equals("") && !siteid.equals("")) {
				List<TsUser> userList = userDAO.selectUserBySiteIdAndDeptid(siteid, deptid);
				for (TsUser user: userList) {
					list.addAll(userSummary(user.getUserid(), startdate, enddate));
				}
			} else if (!siteid.equals("") && deptid.equals("")) {
				List<TsUser> userList = userDAO.selectUserBySiteId(siteid);
				for (TsUser user: userList) {
					list.addAll(userSummary(user.getUserid(), startdate, enddate));
				}
			}
		}
		return list;
	}
	
	private List<ReportSummary> userSummary(String userid, Date startdate, Date enddate) {
		List<ReportSummary> summaryList = new ArrayList<ReportSummary>();
		List<TblCheckReport> list = reportDAO.selectAll(userid,null, null, startdate, enddate);
		double offworkday = 0;
		int latenum = 0;
		long latemin = 0;
		long earlymin = 0;
		int earlynum = 0;
		int weekend = 0;
		double shouldday = 0;
		double realday = 0;
		double holidaynum = 0;
		double leaveday = 0;
		String sitename = "";
		String username = "";
		String deptId = "";
		if (list.size() > 0) {
			for (TblCheckReport report: list) {
				offworkday  += report.getOffworknum();
				holidaynum += report.getHolidaynum();
				latenum += report.getLatenum();
				earlynum += report.getEarlynum();
				leaveday += report.getLeavenum();
				if (report.getShouldHours() == 0.0 && report.getHolidaynum() == 0.0) {
					weekend++;
				}
				latemin += report.getfLateMin() + report.getsLateMin() + report.gettLateMin();
				earlymin += report.getfEarlyMin() + report.getsEarlyMin() + report.gettEarlyMin();
				sitename = report.getSitename();
				username = report.getUsername();
				deptId = report.getDeptid();
			}
			shouldday = list.size() - holidaynum - weekend;
			realday = shouldday - offworkday;
			ReportSummary summary = new ReportSummary();
			summary.setUserid(userid);
			summary.setShouldday(shouldday);
			summary.setRealday(realday);
			summary.setOffworkday(offworkday);
			summary.setLatenum(latenum);
			summary.setEarlynum(earlynum);
			summary.setEarlymin(earlymin);
			summary.setLatemin(latemin);
			summary.setHoliday(holidaynum);
			summary.setLeaveday(leaveday);
			summary.setDeptid(deptId);
			summary.setUsername(username);
			summary.setSitename(sitename);
			summaryList.add(summary);
		}
		return summaryList;
	}

	@Override
	public List<TblCheckReport> selectAllReport(int begin, int pageSize, String userid, String siteid, String deptid,
			String startdate, String enddate) {
		return reportDAO.selectTotalReport(begin, pageSize, userid, siteid, deptid, startdate, enddate);
	}

	@Override
	public int selectAllReportCount(String userid, String siteid, String deptid, String startdate, String enddate) {
		return reportDAO.selectTotalReportCount(userid, siteid, deptid, startdate, enddate);
	}

}
